#include "../../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>


bitmq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        emp = std::make_shared<bitmq::ExchangeManager>("./data/meta.db");
    }

    virtual void TearDown() override 
    {
        //emp->clear();
    }
};

TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> map1;
    std::unordered_map<std::string, std::string> map2 = {{"k1", "v1"}, {"k2", "v2"}};
    emp->declareExchange("exchange1", bitmq::ExchangeType::DIRECT, true, false, map1);
    emp->declareExchange("exchange2", bitmq::ExchangeType::DIRECT, true, false, map1);
    emp->declareExchange("exchange3", bitmq::ExchangeType::DIRECT, true, false, map1);
    emp->declareExchange("exchange4", bitmq::ExchangeType::DIRECT, true, false, map1);
    emp->declareExchange("exchange5", bitmq::ExchangeType::DIRECT, true, false, map2);
    emp->declareExchange("exchange6", bitmq::ExchangeType::DIRECT, true, false, map2);

    ASSERT_EQ(emp->size(), 6);
}


TEST(exchange_test, select_test)
{
    // ASSERT_EQ(emp->exists("exchange1"), true);
    // ASSERT_EQ(emp->exists("exchange2"), false);
    // ASSERT_EQ(emp->exists("exchange3"), true);
    // ASSERT_EQ(emp->exists("exchange4"), true);
    // ASSERT_EQ(emp->exists("exchange5"), true);
    bitmq::Exchange::ptr exp1 = emp->selectExchange("exchange3");
    ASSERT_EQ(exp1->name, "exchange3");
    ASSERT_EQ(exp1->type, bitmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp1->durable, true);
    ASSERT_EQ(exp1->auto_delete, false);
    ASSERT_EQ(exp1->getArgs(), "");

    bitmq::Exchange::ptr exp2 = emp->selectExchange("exchange5");
    ASSERT_EQ(exp2->name, "exchange5");
    ASSERT_EQ(exp2->type, bitmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp2->durable, true);
    ASSERT_EQ(exp2->auto_delete, false);
    ASSERT_EQ(exp2->getArgs(), "k1=v1&k2=v2");
}

// TEST(exchange_test, remove_test)
// {
//     emp->deleteExchange("exchange2");
//     bitmq::Exchange::ptr exp = emp->selectExchange("exchange2");
//     ASSERT_EQ(exp.get(), nullptr);
//     ASSERT_EQ(emp->exists("exchange2"), false);
// }

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}